package com.stone.ble.database;

import android.database.Cursor;
import android.text.TextUtils;

import com.stone.ble.DataModel;
import com.stone.ble.MyConstants;
import com.stone.ble.StoneApplication;

public class DatabaseUtil {

	public static boolean isBindDevice(int top, int sub) {
		DBController controller = StoneApplication.getInstance()
				.getmDBController();
		controller.open();
		StringBuilder sb = new StringBuilder();
		sb.append(DataModel.COLUMN_UI_TOP);
		sb.append(" = ");
		sb.append(top);
		sb.append(" AND ");
		sb.append(DataModel.COLUMN_UI_SUB);
		sb.append(" = ");
		sb.append(sub);
		sb.append(";");
		Cursor cur = controller.query(DataModel.STONE_TABLE_NAME, null,
				sb.toString());
		if (cur.moveToNext()) {
			DataModel data = new DataModel(top, sub);
			data.setSwitch_type(cur.getInt(cur
					.getColumnIndex(DataModel.COLUMN_SWITCH_TYPE)));
			data.setAddress(cur.getString(cur
					.getColumnIndex(DataModel.COLUMN_ADDRESS)));
			if (!TextUtils.isEmpty(data.getAddress())) {
				return true;
			}
		}
		if (cur != null) {
			cur.close();
		}
		controller.close();
		return false;
	}

	public static String getBindedAddress(int top, int sub) {
		String address = "";
		DBController controller = StoneApplication.getInstance()
				.getmDBController();
		controller.open();
		StringBuilder sb = new StringBuilder();
		sb.append(DataModel.COLUMN_UI_TOP);
		sb.append(" = ");
		sb.append(top);
		sb.append(" AND ");
		sb.append(DataModel.COLUMN_UI_SUB);
		sb.append(" = ");
		sb.append(sub);
		sb.append(";");
		Cursor cur = controller.query(DataModel.STONE_TABLE_NAME, null,
				sb.toString());
		if (cur.moveToNext()) {
			DataModel data = new DataModel(top, sub);
			data.setSwitch_type(cur.getInt(cur
					.getColumnIndex(DataModel.COLUMN_SWITCH_TYPE)));
			data.setAddress(cur.getString(cur
					.getColumnIndex(DataModel.COLUMN_ADDRESS)));
			address = data.getAddress();
		}
		if (cur != null) {
			cur.close();
		}
		controller.close();
		return address;
	}

	public static boolean bindDevice(int top, int sub, int type, String address) {
		DBController controller = StoneApplication.getInstance()
				.getmDBController();
		controller.open();
		StringBuilder sb = new StringBuilder();
		sb.append(DataModel.COLUMN_UI_TOP);
		sb.append(" = ");
		sb.append(top);
		sb.append(" AND ");
		sb.append(DataModel.COLUMN_UI_SUB);
		sb.append(" = ");
		sb.append(sub);
		sb.append(";");
		DataModel data = new DataModel(top, sub);
		data.setSwitch_type(type);
		data.setAddress(address);
		boolean res = controller.update(DataModel.STONE_TABLE_NAME,
				sb.toString(), data.toContentValues());
		controller.close();
		return res;
	}

	public static DataModel getDataInfo(int top, int sub) {
		int type = MyConstants.TYPE_LEFT;
		String address = "";
		DBController controller = StoneApplication.getInstance()
				.getmDBController();
		controller.open();
		StringBuilder sb = new StringBuilder();
		sb.append(DataModel.COLUMN_UI_TOP);
		sb.append(" = ");
		sb.append(top);
		sb.append(" AND ");
		sb.append(DataModel.COLUMN_UI_SUB);
		sb.append(" = ");
		sb.append(sub);
		sb.append(";");
		Cursor cur = controller.query(DataModel.STONE_TABLE_NAME, null,
				sb.toString());
		if (cur.moveToNext()) {
			type = cur.getInt(cur.getColumnIndex(DataModel.COLUMN_SWITCH_TYPE));
			address = cur.getString(cur
					.getColumnIndex(DataModel.COLUMN_ADDRESS));
		}
		DataModel data = new DataModel(top, sub);
		data.setSwitch_type(type);
		data.setAddress(address);
		if (cur != null) {
			cur.close();
		}
		controller.close();
		return data;
	}

}
